﻿Imports System.Data.SqlClient

Module LoginService

    ''' <summary>
    '''  判断登录用户是否合法
    ''' </summary>
    ''' <param name="name"></param>
    ''' <param name="pwd"></param>
    ''' <param name="type"></param> 0:为学生 1:为老师
    ''' <returns></returns>
    Public Function isValidUser(ByVal name As String, ByVal pwd As String, ByVal type As Integer) As Boolean
        Dim cn As SqlConnection = getDbConnection()
        If cn Is Nothing Then
            MsgBox("数据库连接失败", MsgBoxStyle.OkOnly, "警告")
            Return False
        End If

        Dim sqlStr As String = ""
        Dim cmd As New SqlCommand(sqlStr, cn)
        Dim userId As String

        If type = 0 Then
            sqlStr = "select id from student where login_name=@name and pwd=@pwd "
        ElseIf type = 1 Then
            sqlStr = "select id from teacher where login_name=@name and pwd=@pwd "
        Else
            Return False
        End If


        cmd.CommandText = sqlStr
        cmd.Parameters.AddWithValue("@name", name)
        cmd.Parameters.AddWithValue("@pwd", pwd)

        userId = cmd.ExecuteScalar()
        ExamGlobal.TEACHER_ID = userId
        Return isNotNull(userId)

    End Function
End Module
